//
//  MainTabBarController.m
//  cm
//
//  Created by suyibaishan on 2017/10/25.
//  Copyright © 2017年 feihu. All rights reserved.
//

#import "MainTabBarController.h"
#import "BaseNavigationController.h"
#import "LoginViewController.h"

#define kClassKey   @"mainVCClassString"
#define kTitleKey   @"title"
#define kImgKey     @"imageName"
#define kSelImgKey  @"selectedImageName"
#define kThemeColor [UIColor colorWithRed:0.19 green:0.53 blue:0.98 alpha:1]

@interface MainTabBarController () <UITabBarControllerDelegate>

@end

@implementation MainTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tabBar.barTintColor = [UIColor whiteColor];
    self.tabBar.translucent = NO;
    self.delegate = self;
    
    NSArray *mainItemsArray = @[
                                @{kClassKey  : @"HomeViewController",
                                  kTitleKey  : @"首页",
                                  kImgKey    : @"home",
                                  kSelImgKey : @"home_selected"},
                                
                                @{kClassKey  : @"ShoppingMallViewController",
                                  kTitleKey  : @"商城",
                                  kImgKey    : @"mall",
                                  kSelImgKey : @"mall_selected"},
                                
                                @{kClassKey  : @"ShoppingCartViewController",
                                  kTitleKey  : @"购物车",
                                  kImgKey    : @"shopping",
                                  kSelImgKey : @"shopping_selected"},
                                
                                @{kClassKey  : @"UserViewController",
                                  kTitleKey  : @"我的",
                                  kImgKey    : @"user",
                                  kSelImgKey : @"user_selected"}];
    
    [mainItemsArray enumerateObjectsUsingBlock:^(NSDictionary *dict, NSUInteger idx, BOOL *stop) {
        UIViewController *viewController = [NSClassFromString(dict[kClassKey]) new];
        //        viewController.title = dict[kTitleKey];
        BaseNavigationController *navController = [[BaseNavigationController alloc] initWithRootViewController:viewController];
        UITabBarItem *item = navController.tabBarItem;
        item.title = dict[kTitleKey];
        item.image = [[UIImage imageNamed:dict[kImgKey]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        item.selectedImage = [[UIImage imageNamed:dict[kSelImgKey]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        [item setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:1]} forState:UIControlStateNormal];
        [item setTitleTextAttributes:@{NSForegroundColorAttributeName : kThemeColor} forState:UIControlStateSelected];
        [self addChildViewController:navController];
    }];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showHomeView) name:@"SHOWHOMEVIEW" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showMallView) name:@"SHOWMALLVIEW" object:nil];
}


- (void)showHomeView
{
    self.selectedIndex = 0;
}

- (void)showMallView
{
    self.selectedIndex = 1;
}

#pragma mark - UITabBarControllerDelegate
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
    if (viewController == self.viewControllers[2] || viewController == self.viewControllers[3]) {
        if (![[mUserDefaults objectForKey:@"isLogin"] isEqualToString:@"1"]) {
            dispatch_async(dispatch_get_main_queue(), ^{
                BaseNavigationController *navController = [[BaseNavigationController alloc] initWithRootViewController:[LoginViewController new]];
                [mAppDelegate.window.rootViewController presentViewController:navController animated:NO completion:nil];
            });
        }
    }
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
